package com.woniuxy.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.entity.UserStats;
import com.woniuxy.core.service.UserStatsService;
import com.woniuxy.core.mapper.UserStatsMapper;
import org.springframework.stereotype.Service;

/**
 * @author 35123
 * @description 针对表【user_stats(用户统计信息表)】的数据库操作Service实现
 * @createDate 2025-07-11 12:22:44
 */
@Service
public class UserStatsServiceImpl extends ServiceImpl<UserStatsMapper, UserStats>
        implements UserStatsService {

    @Override
    public UserStats getUserStatsByUserId(Long userId) {
        QueryWrapper<UserStats> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("user_id", userId);

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public void initUserStats(Long userId) {
        UserStats userStats = new UserStats();
        userStats.setUserId(userId);
        userStats.setLikeCount(0L);
        userStats.setFollowCount(0L);
        userStats.setFanCount(0L);
        userStats.setPostCount(0L);
        save(userStats);
    }

    @Override
    public void incrementPostCount(Long userId) {
        update()
                .setSql("post_count = post_count + 1")
                .eq("user_id", userId)
                .update();
    }

    @Override
    public void decrementPostCount(Long userId) {
        update()
                .setSql("post_count = post_count - 1")
                .eq("user_id", userId)
                .update();
    }
}




